package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.WebVisitMapper;
import com.itheima.pojo.TWebVisit;
import com.itheima.service.WebVisitService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WebVisitServiceImpl implements WebVisitService {
    @Autowired
    private WebVisitMapper webVisitMapper;

    //获取用户访问列表
    @Override
    public Page<TWebVisit> getList(TWebVisit tWebVisit) {
        String keyword = tWebVisit.getKeyword();
        if (keyword.equals("")){
            keyword=null;
        }
        PageHelper.startPage(tWebVisit.getCurrentPage(), tWebVisit.getPageSize());
        Example example = new Example(TWebVisit.class);
        Example.Criteria criteria = example.createCriteria();
        if (keyword != null && !keyword.equals("")) {
            criteria.orLike("ipSource", "%" + keyword + "%");
            criteria.orLike("os", "%" + keyword + "%");
            criteria.orLike("behavior", "%" + keyword + "%");
            criteria.orLike("browser", "%" + keyword + "%");
            criteria.orLike("otherData", "%" + keyword + "%");
        }
        String startTime = tWebVisit.getStartTime();
        if (startTime.equals("")){
            startTime=null;
        }
        if (startTime != null && !startTime.equals("")){
            Example.Criteria criteria1 = example.createCriteria();

            System.out.println("11111111"+startTime);
            String[] split = startTime.split(",");
            Date date1 = DateUtils.str2Date(split[0]);
            String time1 = DateUtils.dateTimeToStr(date1);

            Date date2 = DateUtils.str2Date(split[1]);
            String time2 = DateUtils.dateTimeToStr(date2);
            criteria1.andBetween("createTime",time1,time2);
            example.and(criteria1);
//            criteria.andLessThanOrEqualTo("createTime", time2);
//            criteria.andGreaterThanOrEqualTo("createTime", time1);
        }
        List<TWebVisit> list = webVisitMapper.selectByExample(example);

        if(list != null){
            for (TWebVisit webVisit : list) {
                webVisit.setBehaviorContent(webVisit.getBehavior());
                webVisit.setContent(webVisit.getOtherData());
            }
        }
        return (Page<TWebVisit>) list;
    }

    private Example createExample(String keywords) {
        Example example = new Example(TWebVisit.class);
        Example.Criteria criteria = example.createCriteria();
        if (keywords != null) {
            if (keywords != null && !"".equals(keywords)) {
                criteria.orLike("ipSource", "%" + keywords + "%");
            }
        }
        if (keywords != null) {
            if (keywords != null && !"".equals(keywords)) {
                criteria.orLike("os", "%" + keywords + "%");
            }
        }
        if (keywords != null) {
            if (keywords != null && !"".equals(keywords)) {
                criteria.orLike("behavior", "%" + keywords + "%");
            }
        }
        if (keywords != null) {
            if (keywords != null && !"".equals(keywords)) {
                criteria.orLike("otherData", "%" + keywords + "%");
            }
        }
        if (keywords != null) {
            if (keywords != null && !"".equals(keywords)) {
                criteria.orLike("browser", "%" + keywords + "%");
            }
        }
        return example;

    }
}
